package com.jacky.webflux.jwt.service;

import com.jacky.webflux.jwt.models.CustomerUserDetail;
import com.jacky.webflux.jwt.models.UserModel;
import com.jacky.webflux.jwt.reposities.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
public class UserDetailsService implements ReactiveUserDetailsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserDetailsService.class);
    private final UserRepository userRepository;

    public UserDetailsService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        return userRepository.findByUserName(username)
                .map(CustomerUserDetail::new);
    }

    public Mono<UserModel> saveUser(UserModel model) {
        return userRepository.save(model);
    }
}
